package agenda;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;


public class LesAgendas implements Serializable {
	
	private Map<String,Agenda> agendas;
	
	public LesAgendas() {
		super();
		this.agendas = new HashMap<>();
	}
	
	public void addAgenda(String login){
		agendas.put(login, new Agenda(login));
	}
	public void suppAgenda(String login){
		agendas.remove(login);
	}
	public Agenda getAgenda(String login){
		return agendas.get(login);
	}
	
	public LesAgendas chargerAgendas(String file) throws RemoteException {
		System.out.println("testddddd");
		LesAgendas agen=null;
		try {
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			System.out.println(this.getClass().getSimpleName());
			agen = (LesAgendas) ois.readObject();
			ois.close();
			fis.close();	
			System.out.println("testa");
			return agen;
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("testd");
			return null;
		}
		
	}
	
	public void sauverAgendas(String file) throws RemoteException{
		try {
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			System.out.println(this.getClass().getSimpleName());
			oos.writeObject(this);
			oos.close();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	
	public Map<String, Agenda> getAgendas() throws RemoteException{
		return this.agendas;
	}
}
